package com.starbaba.spreadChannelManager.view;

import java.io.PrintWriter;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.google.gson.JsonObject;

import org.apache.commons.lang3.StringUtils;

import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.spreadChannelManager.model.ProductCodeModel;
import com.starbaba.spreadChannelManager.model.SpreadChannelModel;
import com.starbaba.spreadChannelManager.service.SpreadChannelService;


public class SpreadChannelManagerAction extends BaseAction{

	
	private SpreadChannelService service = new SpreadChannelService();
	
	//查询
	public void querySpreadChannelDetail() {
		
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		
		parsePageRequest();
		
		String prdName = (String)paramsMap.get("channel_name");
		
		try {
		
			PageResponse pageResponse = new PageResponse();
			BaseDao dao = new BaseDao("tt1");
			
			StringBuffer sbSql = new StringBuffer();
			sbSql.append("select prd_id as prdId, prd_name as prdName,spread_link as spreadLink");
			sbSql.append(" from loan.spread_channel_manager");
			sbSql.append(" where 1=1");
			sbSql.append(" and status = 1");
			
			if (prdName != null) {
				sbSql.append(" and prd_name like '%" + prdName +"%'");
			}
			
			//获得页面信息
			int pageSize = pageRequest.getIDisplayLength();
			int pageIndex = pageRequest.getIDisplayStart();
			/*
			 * 分页查询
			 * */
			String limitSql = sbSql.toString() + " LIMIT " + pageIndex + ", " + pageSize;
			List<SpreadChannelModel> dataList = dao.list(limitSql, SpreadChannelModel.class);
			
			//产品渠道ID列表
			ServletActionContext.getRequest().getSession().setAttribute("codeList", getCodeList());
			
			pageResponse.setAaData((List)dataList);
			pageResponse.setSEcho(pageRequest.getSEcho());
			//页面显示记录数，总记录数
			List tmpDataList = dao.list(sbSql.toString(), SpreadChannelModel.class);
			pageResponse.setITotalDisplayRecords(tmpDataList.size());
			pageResponse.setITotalRecords(tmpDataList.size());
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
			
		} catch (Exception e) {
			e.printStackTrace();
			LogService.error("getGeneraldata", e);
		}
		
	}
	
	//获得comm_product_pdid表的产品渠道id
	public List<ProductCodeModel> getCodeList() throws AppException {
		
		BaseDao dao = new BaseDao("tt1");
		
		String codeSql = "select id,prdid,name from comm_product_prdid";
		
		List<ProductCodeModel> list = dao.list(codeSql, ProductCodeModel.class);
		
		return list;
	}
	
	//更新或删除时回显数据
	public String getInfo() throws AppException {
		
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);  //将请求参数封装带paramsMap中
		//在基础上添加产品列表
		BaseDao baseDao = new BaseDao(Constant.POOL_LIFE);
		
		String op = (String)paramsMap.get("op");
		paramsMap.put("op", op);
		if ("1".equals(op) || "2".equals(op)) {//更新或者删除
	
			String name = (String)paramsMap.get("name");
			String prdId = (String)paramsMap.get("prdId");
			
			String sql = "select id,spread_link as spreadLink from spread_channel_manager where prd_id=?";
			SpreadChannelModel tempModel = baseDao.get(sql, SpreadChannelModel.class, prdId);
			
			ServletActionContext.getRequest().getSession().setAttribute("model", tempModel);
			paramsMap.put("name", name);
			paramsMap.put("prdId", prdId);
			paramsMap.put("spreadLink", tempModel.getSpreadLink());
		}
		return SUCCESS;
	}
	
	//添加推广渠道
	public void addNewChannel() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		
		SpreadChannelModel model = new SpreadChannelModel();
		
		try {
			String prdId = (String) paramsMap.get("prdId");
			String sql = "select isapp,name from comm_product_prdid where prdid= ?";
			
			BaseDao dao = new BaseDao("tt1");
			
			int isapp = 0;
			ProductCodeModel tempModel = dao.get(sql, ProductCodeModel.class, prdId);
			if (tempModel != null) {
				isapp = tempModel.getIsapp();
			}
			
			String shortLink = Constant.COMMON_HOST + "/frontend_loan_service/common?funid=1&appid=3&service=loan_service&isapp="+isapp+"&prdid="+prdId;
			
			
			if (StringUtils.isNotEmpty(prdId)) {
				model.setPrdId(Integer.parseInt(prdId));
			}
			if (StringUtils.isNotEmpty(shortLink)) {
				model.setSpreadLink(shortLink);
			}
			if (tempModel!= null && StringUtils.isNotEmpty(tempModel.getName())) {
				model.setPrdName(tempModel.getName());
			}
			
			model.setStatus(1);
			boolean result = service.addNewChannel(model);
			sendJsonMessage(result ? "1" : "0");
			
		} catch(Exception e) {
			
			e.printStackTrace();
			LogService.error("getGeneraldata", e);
		}
		
	}
	
	public void updateChannel() {
		
		PageResponse pageResponse = new PageResponse();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		
		try {
			
			boolean result = service.updateChannel(paramsMap);
			sendJsonMessage(result ? "1" : "0");
			
		} catch(Exception e) {
			e.printStackTrace();
			LogService.error("getGeneraldata", e);
		}
	}
	
	//删除渠道，状态设为0，不显示
	public void dropChannel() {
		
		PageResponse pageResponse = new PageResponse();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		
		try {
			boolean result = service.dropChannel(paramsMap);
			sendJsonMessage(result ? "1" : "0");
		} catch (Exception e) {
			e.printStackTrace();
			LogService.error("getGeneraldata", e);
		}
		
	}
	
	//判断渠道id是否存在
	public void isExist() {
		
		PageResponse pageResponse = new PageResponse();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		int prdId = Integer.parseInt((String) paramsMap.get("prdId"));
		String prdName = (String) paramsMap.get("prdName");
	
		try {
			boolean result = service.isExist(prdId,prdName);
			
			if(result == true) {
				
				PrintWriter pw = ServletActionContext.getResponse().getWriter();
				String judge = "true";
				pw.write(judge);
				pw.flush();
				pw.close();
			}
			
		} catch(Exception e) {
			e.printStackTrace();
			LogService.error("getGeneraldata", e);
		}
		
	}
	
	//判断是否修改数据
	public void isUpdate() {
		
		PageResponse pageResponse = new PageResponse();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		int id = Integer.parseInt((String) paramsMap.get("id"));
		int prdId = Integer.parseInt((String) paramsMap.get("prdId"));
	
		try {
			boolean result = service.isUpdate(id,prdId);
			
			if (result == true) {
				
				PrintWriter pw = ServletActionContext.getResponse().getWriter();
				String judge = "true";
				pw.write(judge);
				pw.flush();
				pw.close();
			}
			
		} catch(Exception e) {
			e.printStackTrace();
			LogService.error("getGeneraldata", e);
		}
		
	}
	

}
